Java.lang.StrictMath 类

java.lang.StrictMath.multiplyExact() 方法返回其参数的乘积。如果结果溢出 long,则会引发异常。

语法

public static long multiplyExact(long x, long y)  

参数

x 指定第一个值。
y 指定第二个值。

返回值

返回其参数的乘积。

异常

抛出ArithmeticException ,如果结果溢出 long。

示例:

在下面的示例中,multiplyExact() 方法用于将给定数字相乘.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  long x = 12;
  long y = 17;
  long p = 145;
  long q = 139;

  System.out.println(StrictMath.multiplyExact(x, y)); 
  System.out.println(StrictMath.multiplyExact(p, q));    
 }
}

上述代码的输出将是:

204
20155